/***********************************************************************
 * Module:  ScrollState.java
 * Author:  Nikola
 * Purpose: Defines the Class ScrollState
 ***********************************************************************/

package genedit.controller;

import genedit.view.graph.GraphWorkArea;

import java.awt.Cursor;
import java.awt.Image;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;

public class ScrollState extends State {
	
	private Point2D start;
   /** @param e */
   public void mouseReleased(MouseEvent e) {
	   Toolkit toolkit = Toolkit.getDefaultToolkit();
		Image image = toolkit.getImage("resources/icons/openhand.png");
		Point hotSpot = new Point(0, 0);
		Cursor cursor = toolkit.createCustomCursor(image, hotSpot,
				"CursorProba");
	   context.setCursor(cursor);
   }
   
   /** @param e */
   public void mouseDragged(MouseEvent e) {
	   	Toolkit toolkit = Toolkit.getDefaultToolkit();
		Image image = toolkit.getImage("resources/icons/closedhand.png");
		Point hotSpot = new Point(0, 0);
		Cursor cursor = toolkit.createCustomCursor(image, hotSpot,
				"CursorProba");
	   context.setCursor(cursor);
	  
	   Point2D lastPosition = e.getPoint();
	   context.pointToUserSpace(lastPosition);
	   double xDiff = start.getX() - lastPosition.getX();
	   double yDiff = start.getY() - lastPosition.getY();
	   
		int valueScrollh = context.getScrollHorizontal().getValue();
		if(((valueScrollh + xDiff >= context.getScrollHorizontal().getMinimum())) && ((valueScrollh + xDiff <= context.getScrollHorizontal().getMaximum()))){										
			context.getScrollHorizontal().setValue((int)(valueScrollh + xDiff));
			valueScrollh = context.getScrollHorizontal().getValue();
			context.sethScrollValue(valueScrollh);
			if(xDiff > 0) {
				double x = xDiff;
				while(x > xDiff) {
					context.setTranslateX(context.getTranslateX() - 1);
					x--;
				}
			} else {
				double x = xDiff;
				while(x > xDiff) {
					context.setTranslateX(context.getTranslateX() + 1);
					x--;
				}
			}
		}
		int valueScrollv = context.getScrollVertical().getValue();
		if(((valueScrollv + yDiff >= context.getScrollVertical().getMinimum())) && ((valueScrollv + yDiff <= context.getScrollVertical().getMaximum()))){					
			context.getScrollVertical().setValue((int)(valueScrollv + yDiff));
			valueScrollv = context.getScrollVertical().getValue();
			context.setvScrollValue(valueScrollv);
			if(yDiff > 0) {
				double x = yDiff;
				while(x > yDiff) {
					context.setTranslateY(context.getTranslateY() - 1);
					x--;
				}
			} else {
				double y = yDiff;
				while(y > yDiff) {
					context.setTranslateY(context.getTranslateY() + 1);
					y--;
				}
			}
		}
		context.setupTransformation();
		context.repaint();
   }
   
   /** @param e */
   public void keyPressed(KeyEvent e) {
      // TODO: implement
   }
   
   /** @param context */
   public ScrollState(GraphWorkArea context) {
	   super(context);
   }
   
   public void mousePressed(MouseEvent e) {
	   start = e.getPoint();
	   context.pointToUserSpace(start);
	   Toolkit toolkit = Toolkit.getDefaultToolkit();
		Image image = toolkit.getImage("resources/icons/closedhand.png");
		Point hotSpot = new Point(0, 0);
		Cursor cursor = toolkit.createCustomCursor(image, hotSpot,
				"CursorProba");
	   context.setCursor(cursor);
	   
   }

}